<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>用户信息管理</title>
    <!-- Bootstrap Styles-->
    <link href="../../css/bootstrap/bootstrap.css" rel="stylesheet"/>
    <!-- FontAwesome Styles-->
    <link href="../../css/font-awesome.css" rel="stylesheet"/>
    <!-- Morris Chart Styles-->

    <!-- Custom Styles-->
    <link href="../../css/custom-styles.css" rel="stylesheet"/>
    <!-- Google Fonts-->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'/>
    <!-- TABLE STYLES-->
    <link href="../../js/dataTables/dataTables.bootstrap.css" rel="stylesheet"/>
</head>

<body>
<div id="wrapper">
    <nav class="navbar navbar-default top-navbar" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="../teacher/tea_course_index.html">题库与考试</a>
        </div>

        <ul class="nav navbar-top-links navbar-right">

            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">
                    <i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
                </a>
                <ul class="dropdown-menu dropdown-user">
                    <li>
                        <a href="../manager/adm_person_index.html"><i class="fa fa-user fa-fw"></i> 个人中心</a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="../../login.html"><i class="fa fa-sign-out fa-fw"></i> 退出登录</a>
                    </li>
                </ul>
                <!-- /.dropdown-user -->
            </li>
            <!-- /.dropdown -->
        </ul>

    </nav>
    <!--/. NAV TOP  -->
    <nav class="navbar-default navbar-side" role="navigation">
        <div class="sidebar-collapse">
            <ul class="nav" id="main-menu">
                <li>
                    <a href="../manager/adm_index.html"><i class="fa fa-dashboard"></i> 用户角色管理</a>
                </li>
                <li>
                    <a href="../manager/adm_infoManage_index.html"><i class="fa fa-desktop"></i> 用户信息管理</a>
                </li>
                <li>
                    <a href="../manager/adm_schoolClass_index.html"><i class="fa fa-bar-chart-o"></i> 系部信息管理</a>
                </li>
                <li>
                    <a href="../manager/adm_exerciseType_index.html"><i class="fa fa-sitemap"></i> 试题类型管理</a>
                </li>
            </ul>
        </div>

    </nav>
    <!-- /. NAV SIDE  -->

    <div id="page-wrapper">
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <h1 class="page-header">

                        用户信息管理
                        <a href="#mymodal_3" role="button" data-toggle="modal">
                            <div style="display: inline;float: right;">
                                <button type="button" class="btn btn-primary">+添加用户</button>
                            </div>
                        </a>

                    </h1>
                    <div class="form-group">

                        <input type="file">
                    </div>
                </div>
            </div>
            <!-- /. ROW  -->

            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            用户列表
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>姓名</th>
                                        <th>学院</th>
                                        <th>性别</th>
                                        <th>手机</th>
                                        <th>角色</th>
                                        <th>状态</th>
                                        <th style="width: 280px;">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>

                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>
            <!-- /. ROW  -->
        </div>
        <!-- /. PAGE INNER  -->
    </div>


    <!-- 模态框1  -->

    <div class="modal fade" id="mymodal_1" tabindex="-1" role="dialog" aria-labelledby="myModelLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModal_1Label">
                        修改用户角色
                    </h4>
                </div>
                <div class="modal-body">
                    <select name="" id="updateUserType" class="form-control">
                        <option value="1">教师</option>
                        <option value="2">学生</option>
                        <option value="3">管理员</option>
                    </select>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-primary" id="updateUserTypeBtn">
                        提交更改
                    </button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal -->
    </div>
    <!--更改状态-->
    <!--修改用户状态-->
    <div class="modal fade" id="mymodal_2" tabindex="-1" role="dialog" aria-labelledby="myModelLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModal_2Label">
                        修改用户状态
                    </h4>
                </div>
                <div class="modal-footer">
                    <select name="" id="updateUserState" class="form-control">
                        <option value="1">待审核</option>
                        <option value="2">审核通过</option>
                        <option value="3">禁用</option>
                    </select>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-primary" id="updateUserStateBtn">
                        提交更改
                    </button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal -->
    </div>
    <!-- 模态框3 -->
    <!--增加用户的模态框-->
    <div class="modal fade" id="mymodal_3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        添加用户
                    </h4>
                </div>
                <div class="modal-body">
                    <input type="text" class="form-control" id="addName" placeholder="用户名">
                    <br>
                    <input type="text" class="form-control" id="addRealName" placeholder="真实姓名">
                    <br>
                    <input type="text" class="form-control" id="addWorkId" placeholder="工作号">
                    <br>
                    <select name="" id="addSchool" class="form-control">
                        <option value="1">国服学院</option>
                        <option value="2">人文学院</option>
                        <option value="3">经济与管理学院</option>
                        <option value="4">教育学院</option>
                        <option value="5">沈钧儒法学院</option>
                        <option value="6">外国语学院</option>
                    </select>
                    <br>
                    <select name="" id="usertype" class="form-control">
                        <option value="1">教师</option>
                        <option value="2">学生</option>
                        <option value="3">管理员</option>
                    </select>
                    <br>
                    <select name="" id="addGender" class="form-control">
                        <option value="1">男</option>
                        <option value="2">女</option>
                    </select>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-primary" id="addUserBtn">
                        提交更改
                    </button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal -->
    </div>

    <!-- /. PAGE WRAPPER  -->
    <!-- /. WRAPPER  -->
    <!-- JS Scripts-->
    <!-- jQuery Js -->
    <script src="../../js/jquery-1.10.2.js"></script>
    <!-- Bootstrap Js -->
    <script src="../../js/bootstrap.min.js"></script>
    <!-- Metis Menu Js -->
    <script src="../../js/jquery.metisMenu.js"></script>
    <!-- DATA TABLE SCRIPTS -->
    <script src="../../js/dataTables/jquery.dataTables.js"></script>
    <script src="../../js/dataTables/dataTables.bootstrap.js"></script>
    <script src="../../js/jquerysession.js"></script>
    <script>
        $(document).ready(function () {
            $('#dataTables-example').dataTable();
            if ($.session.get('userid') == null) {
                alert("请先登录！！！");
                window.location.href = '/login.html';
            } else {
                if ($.session.get('usertype') != 3) {
                    alert("您没有访问这个页面的权限！！！");
                    window.location.href = '/login.html';
                }
            }
            //获取所有的当前用户的信息，信息内容分别为：姓名->realname，学院->schoolid，性别，手机，角色->usertype，状态，操作
            $.ajax({
                type: 'get',
                url: '/user/findAll',
                dataType: 'json',
                success: function (data) {
                    $.each(data.data, function (index, obj) {
                        var school, userType, gender, state;
                        var schooleId = obj.schoolid;
                        var usertype = obj.usertype;
                        if (obj.sex == 1) {
                            gender = "男";
                        } else {
                            gender = "女";
                        }
                        if (obj.state == 1) {
                            state = "待审核";
                        } else if (obj.state == 2) {
                            state = "审核通过";
                        } else {
                            state = "被禁用";
                        }
                        //学校和用户角色根据再次使用ajax获取参数
                        $.ajax({
                            type: 'get',
                            url: '/school/findBySchoolId/' + schooleId,
                            dataType: 'json',
                            success: function (data1) {
                                school = data1.data.name;
                                console.log(school);
                                $.ajax({
                                    type: 'get',
                                    url: '/usertype/findOne/' + usertype,
                                    dataType: 'json',
                                    success: function (data2) {
                                        userType = data2.data.usertype;
                                        console.log(userType);
                                        $('#dataTables-example').append('<tr><td>' + obj.id + '</td><td>' + obj.realname + '</td>' +
                                            '<td>' + school + '</td>' + '<td>' + gender + '</td>' + '<td>' + obj.mobilephone + '</td>' +
                                            '<td>' + userType + '</td>' + '<td>' + state + '</td>' +
                                            '<td><button data-target="#mymodal_1" class="btn btn-primary" data-toggle="modal" onclick="Values(' + obj.id + ')">更改用户角色</button>' +
                                            ' <button data-target="#mymodal_2" class="btn btn-primary" data-toggle="modal" onclick="Values(' + obj.id + ')">更改用户状态</button>' +
                                            '</td></tr>');
                                    }
                                });
                            }
                        });
                    })
                }
            })
        });

        //添加用户
        $("#addUserBtn").click(function (e) {
            e.preventDefault();
            var addName = $("#addName").val();
            var addRealName = $("#addRealName").val();
            console.log(addRealName);
            var addWorkId = $("#addWorkId").val();
            var addSchool = $("#addSchool").val();
            var userType = $("#usertype").val();
            var userGender = $("#addGender").val();
            var addPwd;
            if (addName == "" || addRealName == "" || addWorkId == "") {
                alert("请填入必须的内容");
                window.location.href = "adm_infoManage_index.html";
            } else {
                //找到每个学院对应的默认密码
                $.ajax({
                    type: 'get',
                    url: '/school/findBySchoolId/' + addSchool,
                    dataType: 'json',
                    success: function (data) {
                        if (data.msg == "success") {
                            addPwd = data.data.userdefaultpwd;
                            var user = JSON.stringify({
                                "schoolid": addSchool, "username": addName,
                                "password": addPwd, "workid": addWorkId, "realname": addRealName, "sex": userGender,
                                "usertype": userType, "state": 2
                            });
                            console.log(user);
                            $.ajax({
                                type: 'post',
                                url: '/user/addOne',
                                data: user,
                                contentType: "application/json",
                                dataType: 'json',
                                success: function (data2) {
                                    if (data2.msg == "success") {
                                        alert("增加用户成功");
                                    } else {
                                        alert(data2.msg);
                                    }
                                    window.location.href = "adm_infoManage_index.html";
                                }
                            })
                        }
                    }
                });
            }
        });

        var userid;

        function Values(ID) {
            userid = ID;
        }

        $("#updateUserTypeBtn").click(function (e) {
            e.preventDefault();
            var updateUserType = $("#updateUserType").val();
            var updateUser = JSON.stringify({"id":userid,"usertype":updateUserType});
            $.ajax({
                type:'post',
                url:'/user/updateByUserId',
                data:updateUser,
                contentType:'application/json',
                dataType:'json',
                success:function (data) {
                    if (data.msg="success"){
                        alert("修改成功");
                    } else{
                        alert(data.msg);
                    }
                    window.location.href="adm_infoManage_index.html";
                }
            });
        });

        $('#updateUserStateBtn').click(function (e) {
            e.preventDefault();
            var updateUserState = $("#updateUserState").val();
            var updateUser = JSON.stringify({"id":userid,"state":updateUserState});
            $.ajax({
                type:'post',
                url:'/user/updateByUserId',
                data:updateUser,
                contentType:'application/json',
                dataType:'json',
                success:function (data) {
                    if (data.msg=="success"){
                        alert("更改成功");
                    }else{
                        alert(data.msg);
                    }
                    window.location.href="adm_infoManage_index.html";
                }
            });
        })
    </script>
</div>
</body>

</html>